<?php
class RM_UnitCoupons extends RM_Model {
    protected $_name = 'rm_unit_coupons';

    function insertRows(RM_Coupons_Row $coupon, array $unitIDs){
        $this->delete('coupon_id='.$coupon->id); //remove old values
        if ($unitIDs[0] == 0) {
            //all units
            $coupon->global = 1;
            $coupon->save();
            return true;
        } elseif ($coupon->global == 1) {
            $coupon->global = 0;
            $coupon->save();
        }

        foreach ($unitIDs as $unitID) {
            $this->createRow(array(
                'coupon_id' => $coupon->id,
                'unit_id' => $unitID,
            ))->save();
        }
    }

    function getByCoupon(RM_Coupons_Row $coupon){
        return $this->fetchAll($this->select()->where('coupon_id=?', $coupon->id));
    }
}